# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from datetime import datetime

from django.shortcuts import render, redirect
from django.http import JsonResponse

from .models import RoomType, Room, HotTime
from users.models import Customer
from operation.models import HotelOrder


def room_info(request):
    room_status = request.GET.get('room_status')
    if request.method == 'POST':
        floor = request.POST.get('floor')
        num = request.POST.get('num')
        people = request.POST.get('people')
        status = request.POST.get('status')
        type = request.POST.get('type')
        room = Room.objects.create(
            floor=floor,
            num=num,
            people_num=people,
            status=status,
            type=type,
        )
        room.save()
    if room_status:
        room_infos = Room.objects.filter(status=int(room_status))
    else:
        room_infos = Room.objects.all()
    res = {}
    res['room_infos'] = room_infos
    res['active'] = 'room_info'
    res['display'] = 3
    return render(request, 'room_info.html', res)

def room_info_edit(request):
    if request.method == 'POST':
        floor = request.POST.get('floor')
        num = request.POST.get('num')
        people = request.POST.get('people')
        status = request.POST.get('status')
        type = request.POST.get('type')
        id = request.POST.get('id')
        room = Room.objects.get(id=id)
        room.people_num = people
        room.status = status
        room.type = type
        room.save()
    id = request.GET.get('id')
    r = Room.objects.get(id=id)
    res = {}
    res['room'] = r
    res['active'] = 'room_info'
    res['display'] = 3
    return render(request, 'room_info_edit.html', res)

def room_type(request):
    if request.method == 'POST':
        type_num = request.POST.get('num')
        name = request.POST.get('room_type')
        price = request.POST.get('price')
        room = RoomType.objects.create(
            name=name,
            type_num=type_num,
            price=price
        )
        room.save()
    room_types = RoomType.objects.all()
    res = {}
    res['room_types'] = room_types
    res['active'] = 'room_type'
    res['display'] = 3
    return render(request, 'room_type.html', res)

def change_price(request):
    if request.method == 'POST':
        id = request.POST.get('id')
        price = request.POST.get('price')
        room = RoomType.objects.get(id=id)
        room.price = price
        room.save()
    return JsonResponse({'status': 'ok'})

def ajax_room(request):
    res = {}
    arr = []
    for i in range(1, 5):
        tmp = []
        l = statistic_room(i)
        tmp.append(['已入住', l[2]])
        tmp.append(['已预订', l[1]])
        tmp.append(['空房', l[0]])
        arr.append(tmp)
    res['data'] = arr
    res['status'] = 'ok'
    return JsonResponse(res)


def statistic_room(type):
    rooms = Room.objects.filter(type=type)
    vacancy = 0
    reservation = 0
    enter_room = 0
    for room in rooms:
        if room.status == 1:
            vacancy += 1
        if room.status == 2:
            reservation += 1
        if room.status == 5:
            enter_room += 1
    return [vacancy, reservation, enter_room]

def order_money(request):
    id_card = request.POST.get('id_card')
    room_type = request.POST.get('room_type')
    start_time = request.POST.get('start_time')
    end_time = request.POST.get('end_time')
    room_num = request.POST.get('room_num')
    r = RoomType.objects.get(type_num=int(room_type))
    start_time = datetime.strptime(start_time, '%Y-%m-%d')
    end_time = datetime.strptime(end_time, '%Y-%m-%d')
    if room_num:
        infos = HotelOrder.objects.filter(
            room_num=room_num,
            type=2
            ).order_by('s_time')[:1]
        for info in infos:
            if end_time > info.s_time:
                return JsonResponse({'status':'ok', 'msg': '入住条件不满足，此房间'+str(info.s_time)+'已经其他顾客预订'})
    flag = False
    try:
        hot = HotTime.objects.get(type_num=int(room_type))
    except:
        hot = None
    if hot:
        price = hot.price
        if hot.s_time < start_time < hot.e_time:
            flag = True
        if hot.s_time < end_time < hot.e_time:
            flag = True
    day = (end_time - start_time).days
    d = {
        1: 0.8,
        2: 0.9,
        3: 0.95,
        4: 1
    }
    if flag:
        try:
            user_info = Customer.objects.get(id_card=id_card)
            money = day * float(price) * d.get(user_info.rant)
        except:
            money = day * float(price)
    else:
        try:
            user_info = Customer.objects.get(id_card=id_card)
            money = day * float(r.price) * d.get(user_info.rant)
        except:
            money = day * float(r.price)
    res = {}
    res['data'] = money
    res['status'] = 'ok'
    return JsonResponse(res)


def change_roomstatus(request):
    id = request.POST.get('id')
    status = request.POST.get('status')
    Room.objects.filter(id=id).update(status=status)
    return JsonResponse({'status':'ok'})

def room_hot(request):
    res = {}
    if request.method == 'POST':
        type_num = request.POST.get('room_type')
        s_time = request.POST.get('s_time')
        e_time = request.POST.get('e_time')
        price = request.POST.get('money')
        hot = HotTime.objects.create(
            type_num=type_num,
            s_time=datetime.strptime(s_time+' 12', '%Y-%m-%d %H'),
            e_time=datetime.strptime(e_time+' 12', '%Y-%m-%d %H'),
            price=price
        )
        hot.save()
    r = HotTime.objects.all()
    res['active'] = 'room_hot'
    res['display'] = 3
    res['infos'] = r
    return render(request, 'room_hot.html', res)

def delete_hot(request):
    id = request.GET.get('id')
    HotTime.objects.filter(id=id).delete()
    return redirect('/room_hot')
